﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Model;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_User
{
    public partial class Edit : AdminPage
    {
        private PlugNT.Cms.BLL.User curUser = new PlugNT.Cms.BLL.User();
        private GroupAdmin curGroupAdmin = new GroupAdmin();
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            if (!IsPostBack)
            {
                string name = Request.QueryString["name"] == null ? "" : Request.QueryString["name"].Trim();
                InitData(name);
            }
        }

        private void FillPowerList(string power)
        {
            ddlPower.Items.Clear();
            ddlPower.Items.Add(new ListItem("钻石用户", "0"));
            ddlPower.Items.Add(new ListItem("黄金用户", "1"));
            ddlPower.Items.Add(new ListItem("白金用户", "2"));
            ddlPower.Items.Add(new ListItem("普通用户", "3"));
            int tCount=0;
            foreach (ListItem li in ddlPower.Items)
            {
                if (li.Value == power)
                {
                    ddlPower.SelectedIndex = tCount;
                }
                tCount++;
            }
        }

        private void InitData(string name)
        {
            if (name != "")
            {
                UserInfo model =curUser.GetUserInfo(name);
                if (model != null)
                {
                    int tCount = 0;
                    for (int i = 18; i < 100; i++)
                    {
                        ListItem li = new ListItem(i.ToString() + "岁", i.ToString());
                        ddlAge.Items.Add(li);
                        int? age = model.age == null ? 0 : model.age;
                        if (i == age)
                        {
                            ddlAge.SelectedIndex = tCount;
                        }
                        tCount++;
                    }
                    tCount = 0;
                    txtName.Text = model.name;
                    txtName.ReadOnly = true;
                    //txtPass.Text = model.password;
                    txtQq.Text = model.qq;
                    txtEmail.Text = model.email;
                    foreach (ListItem li in ddlSex.Items)
                    {
                        if (li.Value == model.sex)
                        {
                            ddlSex.SelectedIndex = tCount;
                        }
                        tCount++;
                    }
                    FillPowerList(model.power);
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string username = txtName.Text.Trim();
            string errorMessage = "修改用户--<b>[" + username + "]</b>--失败！&z=no";
            bool flag = false;
            if (username != "")
            {
                UserInfo model = curUser.GetUserInfo(username);
                if (model != null)
                {
                    model.qq = txtQq.Text.Trim();
                    model.email = txtEmail.Text.Trim();
                    model.age = Int32.Parse(ddlAge.SelectedValue.Trim());
                    model.sex = ddlSex.SelectedValue.Trim();
                    model.power = ddlPower.SelectedValue.Trim();
                    if (txtPass.Text.Trim() != "")
                    {
                        model.password = curUser.MD5Hasher(txtPass.Text.Trim()); 
                    }
                    flag = curUser.Update(model);
                }
            }
            if (flag)
            {
                Response.Redirect("../Message.aspx?message=修改用户--<b>[" + username + "]</b>--成功！&z=yes");
            }
            else
            {
                Response.Redirect("../Message.aspx?message=" + errorMessage);
            }
        }

    }
}
